package top.weiliang.springboot3.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import top.weiliang.springboot3.base.PageBean;
import top.weiliang.springboot3.base.R;
import top.weiliang.springboot3.dao.TbSoftwareMapper;
import top.weiliang.springboot3.dao.TbSoftwareTypeMapper;
import top.weiliang.springboot3.entity.TbSoftware;
import top.weiliang.springboot3.entity.TbSoftwareType;
import top.weiliang.springboot3.service.TbSoftwareService;
import top.weiliang.springboot3.vo.PageVO;

@Service
@Slf4j
@RequiredArgsConstructor
public class TbSoftwareServiceImpl implements TbSoftwareService {
    private final TbSoftwareMapper tbSoftwareMapper;
    private final TbSoftwareTypeMapper tbSoftwareTypeMapper;



    @Override
    public R<String> insert(TbSoftware tbSoftware) {
        TbSoftwareType check = tbSoftwareTypeMapper.selectById(tbSoftware.getStid());
        if (check == null) {
            return R.fail("软件分类信息错误", null);
        }
        int i = tbSoftwareMapper.insert(tbSoftware);
        return i == 1 ? R.ok("添加成功", null) : R.fail("添加失败",null);
    }

    @Override
    public R<PageVO<TbSoftware>> query(TbSoftware tbSoftware, PageBean pageBean) {
        IPage<TbSoftware> page = new Page<>(pageBean.getPageNumber(), pageBean.getPageSize());
        QueryWrapper<TbSoftware> wrapper = new QueryWrapper<>();
        //软件分类查询
        wrapper.eq(tbSoftware.getStid() != null,
                "stid", tbSoftware.getStid());
        //软件模糊查询
        wrapper.like(StringUtils.hasText(tbSoftware.getSname()),
                "Sname", tbSoftware.getSname());
        page= tbSoftwareMapper.selectPage(page, wrapper);
        return R.ok(page);
    }

    @Override
    public R<TbSoftware> queryBykey(Integer id) {
        TbSoftware tbSoftware = tbSoftwareMapper.selectById(id);
        return tbSoftware == null ?
                R.fail("查询失败", null) : R.ok("查询成功", tbSoftware);
    }

    @Override
    public R<String>update(TbSoftware tbSoftware){
        //效验名称是否存在
        TbSoftwareType check = tbSoftwareTypeMapper.selectById(tbSoftware.getStid());
        if (check == null ){
            return R.fail("班级信息错误",null);
        }

        int i=tbSoftwareMapper.updateById(tbSoftware);
        return i==1 ?R.ok("修改成功",null) :R.fail("修改失败",null);
    }

    @Override
    public R<String>delete(Integer id){
        int i=tbSoftwareMapper.deleteById(id);
        return i==1 ?R.ok("删除成功",null) :R.fail("删除失败",null);
    }


}
